package cn.edu.scau.cmi.suyaoxing.client;

import cn.edu.scau.cmi.suyaoxing.abstractFactory.*;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;


public class AbstractFactoryClient extends Application{
   public static void main(String[] args) {
       launch(args);
    }
   private String[] insurType= {"人身伤亡保险","驾驶员本身伤亡保险","碰撞保险","综合保险"};
   private String[] insurCompany= {"中国人保","中国平安"};
   private int num1,num2;
   private InsuranceCompany insureCom=null;
   private Insurance inType=null;
   @Override
   public void start(Stage primaryStage) {
       GridPane gridpane=new GridPane();
       gridpane.setAlignment(Pos.CENTER);
       gridpane.setHgap(1);
       gridpane.setVgap(1);
       
       Label lb1=new Label("姓名:");
       Label lb2=new Label("学号:");
       Label lb3=new Label("请选择保险:");
       Label lb4=new Label("世贤");
       Label lb5=new Label("11111");
       Label lb6=new Label("请选择保险公司");
       
       ComboBox<String> cbo1=new ComboBox<>();
       ObservableList<String> items1=FXCollections.observableArrayList(insurCompany);
       cbo1.getItems().addAll(items1);
       
       ComboBox<String> cbo2=new ComboBox<>();
       ObservableList<String> items2=FXCollections.observableArrayList(insurType);
       cbo2.getItems().addAll(items2);
       
       Image image=new Image("/res/factory_1.jpg");
       ImageView imageview=new ImageView(image);
       imageview.setFitHeight(200);
       imageview.setFitWidth(400);
       
       TextField tf=new TextField();
       tf.setPrefColumnCount(15);
       Button bt1=new Button("显示保险信息");
       
       gridpane.add(imageview, 0, 0,1,8);
       gridpane.add(lb1, 1, 0);
       gridpane.add(lb4, 2, 0);
       gridpane.add(lb2, 3, 0);
       gridpane.add(lb5, 4, 0);
       gridpane.add(lb3, 3, 3);
       gridpane.add(lb6, 1, 3);
       gridpane.add(cbo1, 2, 3);
       gridpane.add(cbo2, 4, 3);
       gridpane.add(bt1, 3, 7);
       gridpane.add(tf, 1, 7);
       
       
       bt1.setOnAction(new EventHandler<ActionEvent>() {
           @Override
           public void handle(ActionEvent event) {
        	   num1=items1.indexOf(cbo1.getValue());
        	   insureCom=InsuranceCompany.getInsurCom(num1);
        	   if(insureCom!=null) {
        		   num2=items2.indexOf(cbo2.getValue());
        		   switch(num2) {
        		   case 0:inType=insureCom.getBodyInjure();break;
        		   case 1:inType=insureCom.getPersonInjure();break;
        		   case 2:inType=insureCom.getCollision();break;
        		   case 3:inType=insureCom.getCom();break;
        		   }
        		   if(inType!=null)
        		   tf.setText(inType.getInfo());
        		   else
        			   tf.setText("请选择保险类型");
        	   }
        	   else
        		   tf.setText("请选择保险公司");
           }
       });
       
       Scene scene=new Scene(gridpane,1100,500);
       primaryStage.setTitle("实验四抽象工厂方法");
       primaryStage.setScene(scene);
       primaryStage.show();
    }
}
